Passed
Push — master ( e448bf...51995c )
by Paul
07:24 queued 04:56
created

pollux.media.featured.set   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
1
/** global: wp */
2
3
var pollux = {
4
	media: {
5
		featured: {},
6
	},
7
	metabox: {},
8
};
9
10
/**
11
 * @return void
12
 */
13
pollux.media.featured.init = function()
14
{
15
	jQuery( '#postimagediv' )
16
	.on( 'click', '#pollux-set-featured', function( ev ) {
17
		ev.preventDefault();
18
		wp.media.view.settings.post.featuredImageId = Math.round( jQuery( '#featured' ).val() );
19
		pollux.media.featured.frame = wp.media.featuredImage.frame;
20
		pollux.media.featured.frame().open();
21
	})
22
	.on( 'click', '#pollux-remove-featured', function( ev ) {
23
		ev.preventDefault();
24
		pollux.media.featured.frame = wp.media.featuredImage.frame;
25
		pollux.media.featured.set(-1);
26
	});
27
};
28
29
/**
30
 * @return void
31
 */
32
pollux.media.featured.select = function()
33
{
34
	if( !wp.media.view.settings.post.featuredImageId )return;
35
	var selection = this.get( 'selection' ).single();
36
	pollux.media.featured.set( selection ? selection.id : -1 );
37
};
38
39
/**
40
 * @return void
41
 */
42
pollux.media.featured.set = function( id )
43
{
44
	wp.media.view.settings.post.featuredImageId = Math.round( id );
45
	wp.media.post( 'pollux/archives/featured/html', {
46
		_wpnonce: jQuery( '#_wpnonce' ).val(),
47
		post_type: jQuery( '#archive-type' ).val(),
48
		thumbnail_id: id,
49
	}).done( function( html ) {
50
		jQuery( '.inside', '#postimagediv' ).html( html );
51
	});
52
};
53
54
/**
55
 * @return bool
56
 */
57
pollux.metabox.hasValue = function( el )
58
{
59
	if( el.type === 'checkbox' ) {
60
		return el.checked === true;
61
	}
62
	return el.value !== '';
63
};
64
65
/**
66
 * @return void
67
 */
68
pollux.metabox.init = function()
69
{
70
	var depends = document.querySelectorAll( '.rwmb-input [data-depends]' );
71
	[].forEach.call( depends, function( el ) {
72
		var dependency = pollux.metabox.setVisibility( el );
73
		var event = dependency.type === 'checkbox' ? 'change' : 'keyup';
74
		dependency.addEventListener( event, function() {
75
			pollux.metabox.onChangeValue( el );
76
		}, false );
77
	});
78
};
79
80
/**
81
 * @return void
82
 */
83
pollux.metabox.onChangeValue = function( el )
84
{
85
	pollux.metabox.setVisibility( el );
86
};
87
88
/**
89
 * @return element
90
 */
91
pollux.metabox.setVisibility = function( el )
92
{
93
	var dependency = document.getElementById( el.getAttribute( 'data-depends' ));
94
	var field = el.closest( '.rwmb-field' );
95
	if( pollux.metabox.hasValue( dependency )) {
96
		field.classList.remove( 'hidden' );
97
	}
98
	else {
99
		field.classList.add( 'hidden' );
100
	}
101
	return dependency;
102
};
103
104
jQuery(function() {
105
	pollux.media.featured.init();
106
	pollux.metabox.init();
107
});
108